<?php
$id = empty($_GET['id']) ? 0 : intval($_GET['id']);
if($id == 0 && empty($_GET['filename'])){
	go(PUB_DIR.'goods/');
}
$oGoods = new Goods();
if(!empty($_GET['filename'])){
	$goods = $oGoods->get_by_filename($_GET['filename'], '*', true);
}else{
	$goods = $oGoods->get_row($id, '*', true);
}
if(empty($goods)){
	go(PUB_DIR.'goods/');
}
$goods['stars'] = $goods['score'] * 150 / 5;
$goods['weight'] = format_weight_out($goods['weight'], $goods['weightunit']);

/*******************************************************************************
 * 读取相册
 *******************************************************************************/
$albums = $oGoods->get_album_all($goods['id'], '`path`,`alt`');
if(!empty($albums['list'])){
	// 相册
	foreach($albums['list'] as $key => $album){
		if(empty($album['alt']))$album['alt'] = $goods['name'];
		$album['alt'] = str_replace(array("\r", "\n"), '', $album['alt']);
		$albums['list'][$key] = $album;
	}
	$album_first = $albums['list'][0];
	$vars['album_first'] = $album_first;
	$vars['albums'] = $albums['list'];
}

/*******************************************************************************
 * 分类信息
 *******************************************************************************/
$_category_info = $__f_goods_cats['category_list'][$goods['catid']];
$goods['catname']	= $_category_info['name'];

if(empty($_category_info['spec'])){
	$_category_info['spec'] = array();
}
$vars['specs_info'] = json_encode($_category_info['spec']);

// 相关分类，从根分类到当前分类及当前分类的下级分类
if(!empty($_category_info['path'])){
	$_cat_path = explode(',', $_category_info['path']);
	$_cats = $__f_goods_cats['category_tree'];
	$_related_cats = array();
	foreach ($_cat_path as $pid) {
		$_related_cats[$pid] = $_cats[$pid];
		$_cats = $_cats[$pid]['categorys'];
	}
	$_related_cats[$_category_info['id']] = $_category_info;
	if(!empty($_cats[$_category_info['id']]['categorys'])){
		$_cats = $_cats[$_category_info['id']]['categorys'];
		$_related_cats = array_merge($_related_cats, $_cats);
	}
	$vars['related_cats'] = $_related_cats;
}

/*******************************************************************************
 * 标签
 *******************************************************************************/
if($goods['recommend'] > 0){
	$goods['recommend_style'] = Statics::$recommend_style_arr[$goods['recommend']];
}
$goods['heat'] = abs(100 - $goods['price']) * 3 + $goods['hits'] * 7;

// 库存单位
$goods['quantityunit'] = Statics::$goods_quantity_unit_arr[$goods['quantityunit']];

/*******************************************************************************
 * 属性，规格
 *******************************************************************************/
$prototypes = Prototype::get_prototypes();

$vars['attrs'] = $oGoods->get_attrs($id);

// 规格，销售属性
$specs = $oGoods->get_specs($id, true);
$vars['specs'] = $specs['specs'];
$vars['specs_json'] = json_encode($vars['specs']);
if($specs['price']['max'] > $specs['price']['min']){
	if($specs['price']['min'] == 0 || $specs['price']['min'] > $goods['price']){
		$specs['price']['min'] = $goods['price'];
	}elseif($specs['price']['max'] < $goods['price']){
		$specs['price']['max'] = $goods['price'];
	}
	$goods['price_min'] = $specs['price']['min'];
	$goods['price_max'] = $specs['price']['max'];
}
list($goods['integer'], $goods['decimals']) = explode('.', $goods['price']);

$vars['goods'] = $goods;
$vars['category'] = $_category_info;

/*******************************************************************************
 * 品牌故事
 *******************************************************************************/
if(!empty($goods['brand'])){
	$oBrand = new Brand();
	$brandinfo = $oBrand->get_row($goods['brand'], '*');
	if(empty($brandinfo['mark'])){
		$brandinfo['href'] = PUB_DIR.'brand/v/'.$brandinfo['id'].'.html';
	}else{
		$brandinfo['href'] = PUB_DIR.'brand/v/'.$brandinfo['mark'].'.html';
	}
	
	if(!empty($brandinfo['related_news'])){
		$ids = array_slice(explode(',', trim($brandinfo['related_news'], ', ')), 0, 5);	// 最多显示7条
		$oNews = new News();
		$related_news = array();
		foreach($ids as $newsid){
			$news = $oNews->get_row($newsid, '`id`,`title`,`filename`,`addtime`,`updatetime`');
			$news['publishtime'] = empty($news['updatetime']) ? fdate($news['addtime']) : fdate($news['updatetime']);
			if(empty($news['url'])){
				if(empty($news['filename'])){
					$news['url'] = PUB_DIR.'news/'.time_to_path($news['addtime']).'/'.$news['id'].'.html';
				}else{
					$news['url'] = PUB_DIR.'news/v/'.$news['filename'].'.html';
				}
			}
			$related_news[$newsid] = $news;
		}
		$brandinfo['related_news'] = $related_news;
	}
	$vars['brand'] = $brandinfo;
}

/*******************************************************************************
 * 人气组合
 *******************************************************************************/
if(empty($goods['combine_goods'])){
	$vars['combine_price_all'] = $goods['price'];
	$vars['combine_price'] = $goods['price'];
	$vars['combine_price_save'] = 0;
}else{
	$ids = explode(',', trim($goods['combine_goods'], ', '));
	$combine_goods = array();
	$combine_price = 0;
	foreach($ids as $goodsid){
		$the_goods = $oGoods->get_row($goodsid, '`id`,`name`,`cover`,`price`,`recommend`,`filename`,`addtime`');
		$combine_price += $the_goods['price'];
		if($the_goods['recommend'] > 0){
			$the_goods['recommend_style'] = Statics::$recommend_style_arr[$the_goods['recommend']];
		}
		if($__f_sysvars['url_rewrite'] == 'html' || $__f_sysvars['url_rewrite'] == 'rewrite'){
			if(empty($the_goods['filename'])){
				$the_goods['url'] = PUB_DIR.'goods/'.time_to_path($the_goods['addtime']).'/'.$the_goods['id'].'.html';
			}else{
				$the_goods['url'] = PUB_DIR.'goods/'.$the_goods['filename'].'.html';
			}
		}else{
			$the_goods['url'] = WEB_DIR."index.php?mdl=goods&do=view&id={$the_goods['id']}";
		}
		$combine_goods[$goodsid] = $the_goods;

	}
	$vars['combine_goods'] = $combine_goods;
	$vars['combine_price_all'] = $goods['price'] + $combine_price;
	$vars['combine_price'] = empty($goods['combine_price']) || $goods['combine_price'] == 0 ? $vars['combine_price_all'] : $goods['combine_price'];
	$vars['combine_price_save'] = $vars['combine_price_all'] - $vars['combine_price'];
}

/*******************************************************************************
 * 促销信息
 *******************************************************************************/
if(!empty($goods['salesinfo'])){
	if(!isset($oNews))$oNews = new News();
	$salesinfo = $oNews->get_row($goods['salesinfo'], '`id`,`title`,`filename`,`addtime`,`updatetime`');
	$salesinfo['publishtime'] = empty($salesinfo['updatetime']) ? fdate($salesinfo['addtime']) : fdate($salesinfo['updatetime']);
	if(empty($salesinfo['url'])){
		if(empty($salesinfo['filename'])){
			$salesinfo['url'] = PUB_DIR.'news/'.time_to_path($salesinfo['addtime']).'/'.$salesinfo['id'].'.html';
		}else{
			$salesinfo['url'] = PUB_DIR.'news/v/'.$salesinfo['filename'].'.html';
		}
	}
	$vars['salesinfo'] = $salesinfo;
}

/*******************************************************************************
 * 相关新闻
 *******************************************************************************/
if(!empty($goods['related_news'])){
	$ids = explode(',', trim($goods['related_news'], ', '));
	if(!isset($oNews))$oNews = new News();
	$related_news = array();
	foreach($ids as $newsid){
		$news = $oNews->get_row($newsid, '`id`,`title`,`filename`,`addtime`,`updatetime`');
		$news['publishtime'] = empty($news['updatetime']) ? fdate($news['addtime']) : fdate($news['updatetime']);
		if(empty($news['url'])){
			if(empty($news['filename'])){
				$news['url'] = PUB_DIR.'news/'.time_to_path($news['addtime']).'/'.$news['id'].'.html';
			}else{
				$news['url'] = PUB_DIR.'news/v/'.$news['filename'].'.html';
			}
		}
		$related_news[$newsid] = $news;
	}
	$vars['related_news'] = $related_news;
}

/*******************************************************************************
 * 同类商品
 *******************************************************************************/
$same_cat_goods = $oGoods->get_list('*', array('catid' => $goods['catid'], 'kept' => " AND `id`!={$goods['id']}"), null, $page = 1, $size = 4, $offset = 0);
foreach($same_cat_goods['list'] as $key => $the_goods){
	if($the_goods['recommend'] > 0){
		$the_goods['recommend_style'] = Statics::$recommend_style_arr[$the_goods['recommend']];
	}
	if($__f_sysvars['url_rewrite'] == 'html' || $__f_sysvars['url_rewrite'] == 'rewrite'){
		if(empty($the_goods['filename'])){
			$the_goods['url'] = PUB_DIR.'goods/'.time_to_path($the_goods['addtime']).'/'.$the_goods['id'].'.html';
		}else{
			$the_goods['url'] = PUB_DIR.'goods/'.$the_goods['filename'].'.html';
		}
	}else{
		$the_goods['url'] = WEB_DIR."index.php?mdl=goods&do=view&id={$the_goods['id']}";
	}
	$same_cat_goods['list'][$key] = $the_goods;
	$vars['same_cat_goods'] = $same_cat_goods['list'];
}

/*******************************************************************************
 * 包装清单
 *******************************************************************************/
if(!empty($goods['fittings'])){
	$ids = unserialize($goods['fittings']);
	$oFittings = new Fittings();
	$fittings_list = array();
	foreach($ids as $fittingsid => $num){
		$fittings_list[$fittingsid] = $oFittings->get_row($fittingsid, '`id`,`name`');
		$fittings_list[$fittingsid]['num'] = $num;
	}
	$vars['fittings_list'] = $fittings_list;
}

/*******************************************************************************
 * 评价详情
 *******************************************************************************/
if(!empty($goods['scores'])){
	$scores = unserialize($goods['scores']);
	foreach($scores as $type => $row){
		$row['stars'] = $row['score'] * 150 / 5;
		$scores[$type] = $row;
	}
	$vars['scores'] = $scores;
}

/*******************************************************************************
 * 页面信息　& 当前位置
 *******************************************************************************/
$__category_id = $goods['catid'];
if($__f_sysvars['url_rewrite'] == 'html' || $__f_sysvars['url_rewrite'] == 'rewrite'){
	if(empty($goods['filename'])){
		$url = PUB_DIR.'goods/'.time_to_path($goods['addtime']).'/'.$id.'.html';
	}else{
		$url = PUB_DIR.'goods/v/'.$goods['filename'].'.html';
	}
}else{
	$url = WEB_DIR."index.php?mdl=goods&do=view&id={$goods['id']}";
}
url_here($goods['name'], $url);
url_here($goods['catname'], PUB_DIR."goods/{$_category_info['mark']}/");
$__pageinfo['keywords'] = "{$goods['name']},{$goods['price']}元";
$__pageinfo['description'] = $goods['summary'];
?>